package listbyorder.access201_300.test227;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/24 21:07
 */
public class Solution1 {

    // 300栈答案
    // 将字符串s转化为后缀表达式然后计算
    public int calculate(String s) {
        String[] strArr = generate(s);   // 生成后缀表达式的数组
        return calStrArr(strArr);        // 计算出来后缀表达式
    }

    private String[] generate(String s) {
        char[] chars = s.toCharArray();
        int len = chars.length;
        int temp = -1;
        List<String> res = new ArrayList<>();
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < len; i++) {
            if (chars[i] == ' ') {
                continue;
            }
            if (isNum(chars[i])) {
                if (temp == -1) {
                    temp = chars[i] - '0';
                } else {
                    temp = temp * 10 + (chars[i] - '0');
                }
            } else {
                if (temp != -1) {
                    res.add(temp + "");
                    temp = -1;   // temp恢复原来的状态
                }
                while (!stack.isEmpty()) {
                    if (compare(chars[i] + "", stack.peek())) {
                        break;
                    }
                    res.add(stack.pop());
                }
                stack.push(chars[i] + "");
            }
        }
        if (temp != -1) {
            res.add(temp + "");
        }
        while (!stack.isEmpty()) {
            res.add(stack.pop());
        }
        String[] resArr = new String[res.size()];
        for (int i = 0; i < resArr.length; i++) {
            resArr[i] = res.get(i);
        }
        return resArr;
    }

    private boolean compare(String cur, String peek) {
        if (cur.equals("*") || cur.equals("/")) {
            if (peek.equals("+") || peek.equals("-")) {
                return true;
            }
        }
        return false;
    }

    // 检查当前的字符是否是数字
    private boolean isNum(char cur) {
        return cur >= '0' && cur <= '9';
    }

    private int calStrArr(String[] strArr) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < strArr.length; i++) {
            if (strArr[i].equals("+")) {
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num2 + num1);
            } else if (strArr[i].equals("-")) {
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num2 - num1);
            } else if (strArr[i].equals("*")) {
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num2 * num1);
            } else if (strArr[i].equals("/")) {
                int num1 = stack.pop();
                int num2 = stack.pop();
                stack.push(num2 / num1);
            } else {
                stack.push(Integer.parseInt(strArr[i]));
            }
        }
        return stack.pop();
    }
}
